import store from '@/store/index'
import router from "@/router"
import {clearRequestClear} from "@/utils/cacelRequest";

router.beforeEach(async (to, from, next) => {
    if (to.path === '/login') {
        store.commit('CLEAR_CACHE')
        sessionStorage.clear()
        next()
    } else {
        if (sessionStorage.getItem('token')) {
            if (store.getters.getRoutes.length !== 0) {
                clearRequestClear()
                next()
            } else {
                console.time('start')
                const menuList = JSON.parse(sessionStorage.getItem('menuList') || "[]")
                await store.dispatch('convertRoutes', menuList)
                next({...to, replace: true})
                console.timeEnd('start')
            }
        } else {
            next('/login')
        }

    }
})
router.afterEach(to => {
    /**
     * 三级路由无法缓存问题
     * 删除父级视图
     */
    if (to.matched && to.matched.length > 2) {
        to.matched.splice(1, 1)
    }
})
